<?php
    function getCategories()
    {
        $sql = "SELECT id, title, alias, picture, create_date FROM c_ring_category WHERE status=1 ORDER BY position DESC";        
        $result = @mysql_query($sql);    
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;            
        }
        return $rows;
    }
    
    function GetRing($categoryId, $startRecord, $numberRecordPerPage)
    {
        $condition = "";
        $condition .= " AND categoryId=" . $categoryId;
        
        $sql = "SELECT * FROM c_ring_back WHERE status=1 " . $condition . " ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;                
        $result = @mysql_query($sql);    
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;            
        }
        return $rows;    
    }
    
    function GetTotalRing($categoryId)
    {
        $condition = "";
        $condition .= " AND categoryId=" . $categoryId;
        
        $sql = "SELECT count(id) FROM c_ring_back WHERE status=1 " . $condition;
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows["count(id)"];    
    }
    
    function GetRingLastest($startRecord, $numberRecordPerPage)
    {
        $sql = "SELECT * FROM c_ring_back WHERE status=1 ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;                
        $result = @mysql_query($sql);    
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;            
        }
        return $rows;    
    }
    
    function GetTotalRingLastest()
    {
        $sql = "SELECT count(id) FROM c_ring_back WHERE status=1";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows["count(id)"];    
    }
    
    function GetRingTopDownload($startRecord, $numberRecordPerPage)
    {
        $sql = "SELECT * FROM c_ring_back WHERE status=1 ORDER BY download DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;                
        $result = @mysql_query($sql);    
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;            
        }
        return $rows;    
    }
    
    function UpdateHit($id)
    {
        $sql = "UPDATE c_ring_back SET hit=(hit+1) WHERE id=" . $id;
        $result = @mysql_query($sql);            
        return $result;
    }
    
    function GetTotalRingTopDownload()
    {
        $sql = "SELECT count(id) FROM c_ring_back WHERE status=1";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows["count(id)"];    
    }
    
    function GetRingSearch($song_name, $singer_name, $startRecord, $numberRecordPerPage)
    {
        $condition = "";
       
        if(!empty($song_name)){
            $song_name = removeSign($song_name);
            $song_name = str_replace(" ", "%", $song_name);    
            $condition .= " AND title_search LIKE ('%" . mysql_escape_string($song_name) . "%')";
        }
        
        if(!empty($singer_name)){
            $singer_name = removeSign($singer_name);
            $singer_name = str_replace(" ", "%", $singer_name);    
            $condition .= " AND singer_name_search LIKE ('%" . mysql_escape_string($singer_name) . "%')";
        }
            
        $sql = "SELECT * FROM c_ring_back WHERE status=1 " . $condition . " ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;                
        $result = @mysql_query($sql);    
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;            
        }
        return $rows;    
    }
    
    function GetTotalRingSearch($song_name, $singer_name)
    {
        $condition = "";
       
        if(!empty($song_name)){
            $song_name = removeSign($song_name);
            $song_name = str_replace(" ", "%", $song_name);    
            $condition .= " AND title_search LIKE ('%" . mysql_escape_string($song_name) . "%')";
        }
        
        if(!empty($singer_name)){
            $singer_name = removeSign($singer_name);
            $singer_name = str_replace(" ", "%", $singer_name);    
            $condition .= " AND singer_name_search LIKE ('%" . mysql_escape_string($singer_name) . "%')";
        }
        
        $sql = "SELECT count(id) FROM c_ring_back WHERE status=1 " . $condition;
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows["count(id)"];    
    }
    
    function GetRingById($songId)
    {
        $sql = "SELECT * FROM c_ring_back WHERE status=1 AND id=" . $songId;
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);
        return $rows;    
    }
    
    /*
    * Lấy cú pháp sms
    */
    function getSmsSyntax()
    {
        $sql = "SELECT * FROM c_config_sms";        
        $result = @mysql_query($sql);
        $rows = array();
        while($row = @mysql_fetch_assoc($result)){            
            $rows[$row["type"]]["serviceCode"] = $row["serviceCode"];            
            $rows[$row["type"]]["syntax"] = $row["syntax"];            
        }    
        return $rows;   
    }
    
    function getImage($nameImage, $folder, $date='', $type='')
    {        
        if($type!='')
            $nameImage=$type.'_'.$nameImage;
        if($date!='')
            return baseUrl() . "upload/" . $folder . "/" . date("Y", $date) . "/" . date("md", $date) . "/" . $nameImage;
        else
            return baseUrl() . "upload/" . $folder . "/" . $nameImage;
    }
    
    function cleanQuery($string)
    {        
        if(empty($string)) return $string;
        $string = mysql_escape_string(trim($string));
        
        $badWords = array(
            "/Select(.*)From/i"
            , "/Union(.*)Select/i"
            , "/Update(.*)Set/i"
            , "/Delete(.*)From/i"
            , "/Drop(.*)Table/i"
            , "/Insert(.*)Into/i"                
            , "/http/i"
            , "/--/i"
        );
        
        $string = preg_replace($badWords, "", $string);

        return $string;
    }
    
    function removeSign($str) {
        $coDau = array ("à", "á", "ạ", "ả", "ã", "â", "ầ", "ấ", "ậ", "ẩ", "ẫ", "ă", "ằ", "ắ", "ặ", "ẳ", "ẵ", "è", "é", "ẹ", "ẻ", "ẽ", "ê", "ề", "ế", "ệ", "ể", "ễ", "ì", "í", "ị", "ỉ", "ĩ", "ò", "ó", "ọ", "ỏ", "õ", "ô", "ồ", "ố", "ộ", "ổ", "ỗ", "ơ", "ờ", "ớ", "ợ", "ở", "ỡ", "ù", "ú", "ụ", "ủ", "ũ", "ư", "ừ", "ứ", "ự", "ử", "ữ", "ỳ", "ý", "ỵ", "ỷ", "ỹ", "đ", "À", "Á", "Ạ", "Ả", "Ã", "Â", "Ầ", "Ấ", "Ậ", "Ẩ", "Ẫ", "Ă", "Ằ", "Ắ", "Ặ", "Ẳ", "Ẵ", "È", "É", "Ẹ", "Ẻ", "Ẽ", "Ê", "Ề", "Ế", "Ệ", "Ể", "Ễ", "Ì", "Í", "Ị", "Ỉ", "Ĩ", "Ò", "Ó", "Ọ", "Ỏ", "Õ", "Ô", "Ồ", "Ố", "Ộ", "Ổ", "Ỗ", "Ơ", "Ờ", "Ớ", "Ợ", "Ở", "Ỡ", "Ù", "Ú", "Ụ", "Ủ", "Ũ", "Ư", "Ừ", "Ứ", "Ự", "Ử", "Ữ", "Ỳ", "Ý", "Ỵ", "Ỷ", "Ỹ", "Đ", "ê", "ù", "à" );

        $khongDau = array ("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "i", "i", "i", "i", "i", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "u", "u", "u", "u", "u", "u", "u", "u", "u", "u", "u", "y", "y", "y", "y", "y", "d", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "I", "I", "I", "I", "I", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "U", "U", "U", "U", "U", "U", "U", "U", "U", "U", "U", "Y", "Y", "Y", "Y", "Y", "D", "e", "u", "a" );
        return str_replace ( $coDau, $khongDau, $str );
    }
?>
